package com.luyun.frogpay.base.network.request;

import androidx.annotation.Nullable;

import java.io.IOException;

import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

/**
 * 描述：
 *
 * @author 无人区
 * @date 2019/5/6
 */
public class TokenAuthenticator implements Authenticator {
    @Nullable
    @Override
    public Request authenticate(Route route, Response response) throws IOException {
        return null;
    }
//    @Nullable
//    @Override
//    public Request authenticate(Route route, Response response) throws IOException {
//        //取出本地的refreshToken
//        String refreshToken = Hawk.get(SPKey.REFRESH_TOKEN);
//        // 通过一个特定的接口获取新的token，此处要用到同步的retrofit请求
//        Retrofit retrofit = new Retrofit.Builder()
//                //设置数据解析器
//                .addConverterFactory(GsonConverterFactory.create())
//                .baseUrl(Constants.BASE_URL)
//                .build();
//        RetrofitService service = retrofit.create(RetrofitService.class);
//        Call<BaseResponse<RefreshTokenResponse>> call = service.refreshToken(refreshToken);
//        //要用retrofit的同步方式
//        RefreshTokenResponse refreshTokenResponse = call.execute().body().getResult();
//        //如果 refreshToken 过期
//        if (refreshTokenResponse.isValid() == false || StringUtils.isEmpty(refreshTokenResponse.getAccessToken())){
//            ARouter.getInstance().build(RouteActivityPath.LOGIN_ACTIVITY).navigation();
//            return response.request().newBuilder()
//                    .header("Authorization", "Bearer " + Hawk.get(SPKey.TOKEN))
//                    .build();
//        }else {
//            String newToken = refreshTokenResponse.getAccessToken();
//            Log.d("newToken: " + newToken);
//            Hawk.put(SPKey.TOKEN, newToken);
//            return response.request().newBuilder()
//                    .header("Authorization", "Bearer " + newToken)
//                    .build();
//        }
//    }
}
